package hu.arnoldfarkas.camel.dropbox;

import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxWriteMode;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DropboxUploader {

    private final DbxClient client;

    public DropboxUploader(DbxClient client) {
        this.client = client;
    }

    public void uploadFile(String localeFileAbsPath, String dbxFileName) throws FileNotFoundException, DbxException, IOException {
        dbxFileName = dbxFileName.startsWith("/") ? dbxFileName : ("/" + dbxFileName);
        File inputFile = new File(localeFileAbsPath);
        FileInputStream inputStream = new FileInputStream(inputFile);
        try {
            client.uploadFile(
              dbxFileName,
              DbxWriteMode.add(),
              inputFile.length(),
              inputStream
            );
        } finally {
            inputStream.close();
        }
    }

}
